package com.dalao.root.custom.service;

import com.dalao.root.custom.entity.CusField;
import com.dalao.root.custom.entity.CusForm;
import com.dalao.root.custom.entity.CusTable;
import com.dalao.root.custom.mapper.FieldMapper;
import com.dalao.root.custom.mapper.FormMapper;
import com.dalao.root.custom.mapper.TableMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class CustomService implements TableService,FieldService,DataService,FormService{
    protected Logger logger = LoggerFactory.getLogger(CustomService.class);

    @Autowired
    private TableMapper tableMapper;

    @Autowired
    private FieldMapper fieldMapper;

    @Autowired
    private FormMapper formMapper;

    @Override
    public CusField addField(CusField cusField) {
        try {
            fieldMapper.addField(cusField);
            return cusField;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }

    @Override
    public boolean dropField(CusField cusField) {

        try {
            fieldMapper.dropField(cusField);
            return true;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return false;
        }
    }

    @Override
    public CusTable newTable(CusTable cusTable) {
        try {
            tableMapper.createTable(cusTable);
            return cusTable;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return null;
        }
    }

    @Override
    public boolean dropTable(CusTable cusTable) {
        try {
            tableMapper.dropTable(cusTable);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public CusForm findForm(CusTable table) {
        return formMapper.findForm(table);
    }
}
